package com.edu.controller;

import com.edu.component.PageBean;
import com.edu.component.QueryParam;
import com.edu.entity.Question;
import com.edu.service.QuestionService;
import com.edu.utils.CommonUtils;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/Question")
public class QuestionController extends BaseController{
    @Autowired
    private QuestionService questionService;

    /* 方法(1)-局部异常处理器
    *  Fallback: 退路; 兜底的方法, 应变措施
    */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public String exceptionFallBack(Exception e){
        e.printStackTrace();
        return R.error(e).toJson();
    }

    /* 方法(2)-index主页入口 */
    @GetMapping("/index")
    public String index(){
        return "views/question/question_list";
    }
    /* 方法(3)-列表页 */
    @RequestMapping("/listPage")
    @ResponseBody
    public String listPage(HttpServletRequest req){
        //1.提取分页参数，封装为QP对象
        QueryParam QP = new QueryParam(req);
        //2.调用service获取数据
        PageBean PB = questionService.listPage(QP);
        //3.返回PageBean对象
        return PB.toJson();
    }

    /* 方法(4)-跳转到新增题目的视图页面
    *  映射地址：/Question/gotoAddQuestion
    */
    @GetMapping("/gotoAddQuestion")
    public String gotoAddQuestion(){
        return "views/question/question_add";
    }
    /* 方法(5)-新增题目处理入口 */
    @PostMapping("/addQuestion")
    @ResponseBody
    public R addQuestion(@RequestBody Question question){
        //1.获取当前登录用户对象
        Integer userId = CommonUtils.getLoginUser().getId();
        //2.设置userId 到 [question对象]
        question.setCreatorId(userId);
        //3.添加题目到数据库
        questionService.addQuestion(question);
        //4.如果有异常,异常处理器会捕获.(这里不用写异常处理）
        //5.执行成功 ==> 返回[R]
        return R.ok();
    }
    /* 方法(6)-跳转到修改页 */
    @GetMapping("/gotoEditQuestion/{qId}")
    public String gotoEditQuestion(
            @PathVariable("qId") Integer qId, Model model
    ){
        //1.设置题目ID到模型域, 最终放入 ==> 请求域
        model.addAttribute("questionId",qId);
        //2.内部转发到[视图页]
        return "views/question/question_edit";
    }

    /* 方法(7)-获取题目的数据 */
    @GetMapping("/queryQuestionById/{qId}")
    @ResponseBody
    public R queryQuestionById(
            @PathVariable("qId") Integer qId
    ){
        Question question = questionService.queryQuestionByQid(qId);
        return R.data(question);
    }

    /* 方法(8)-修改题目的处理入口 */
    @PostMapping("/updateQuestion")
    @ResponseBody
    public R updateQuestion( @RequestBody Question question ){
        //1.获取登陆者的ID
        Integer userId = CommonUtils.getLoginUser().getId();
        question.setCreatorId(userId);
        questionService.updateQuestion(question);
        return R.ok();
    }

    /* 方法(9)-删除题目 */
    @DeleteMapping("/deleteQuestion/{id}")
    @ResponseBody
    public R deleteQuestion(
            @PathVariable("id") Integer id
    ){
        questionService.deleteQuestion(id);
        return R.ok();
    }
}
